﻿using System;
using System.Collections.Generic;
using System.Data;
using Think9.Models;
using Think9.Services.Base;

namespace Think9.Services.Basic
{
	public class BasicHelp
	{
		/// <summary>
		///
		/// </summary>
		/// <param name="dsindex"></param>
		/// <param name="maxNumber">最大列数</param>
		/// <param name="number">第几行</param>
		/// <param name="from">add或edit用于控制编辑时锁定</param>
		/// <returns></returns>
		public static string GetDisabledStrForGridColumn(DataTable dsindex, int maxNumber, int number, string from)
		{
			string strLock = "";
			string isLock = "";
			string numLocked = "";
			string indexid = "";
			for (int i = 1; i <= maxNumber; i++)
			{
				isLock = "";
				indexid = "v" + i.ToString();
				foreach (DataRow dr in dsindex.Rows)
				{
					if (dr["IndexId"].ToString() == indexid)
					{
						isLock = dr["isLock"].ToString();//是否锁定？1是2否3按行锁定
						numLocked = " " + dr["isLock2"].ToString() + " ";//锁定的行号 以空格间隔
						break;
					}
				}

				//锁定
				if (isLock == "1")
				{
					strLock += "[" + indexid + "]";
				}
				//按行锁定
				if (isLock == "3" && numLocked.Contains(" " + number.ToString() + " "))
				{
					strLock += "[" + indexid + "]";
				}
				//编辑时锁定
				if (isLock == "9" && from != "add")
				{
					strLock += "[" + indexid + "]";
				}
			}

			return strLock;
		}

		public static string GetGridIdByIndexid(string indexid)
		{
			string gridId = "";
			//indexid最多三位数，即v1-v99，以下逻辑没有问题
			if (indexid.EndsWith("v1") || indexid.EndsWith("v2") || indexid.EndsWith("v3") || indexid.EndsWith("v4") || indexid.EndsWith("v5") || indexid.EndsWith("v6") || indexid.EndsWith("v7") || indexid.EndsWith("v8") || indexid.EndsWith("v9"))
			{
				gridId = "tb_" + indexid.Substring(0, indexid.Length - 2);
			}
			else
			{
				gridId = "tb_" + indexid.Substring(0, indexid.Length - 3);
			}

			return gridId;
		}

		/// <summary>
		/// 获得分割后字符
		/// </summary>
		/// <returns></returns>
		public static string GetSplitStrByChar(string value, string _char, int num)
		{
			string str = value;
			if (value.Contains(_char))
			{
				string[] arr = BaseUtil.GetStrArray(value, _char);//.分割
				if (arr[num] != null)
				{
					str = arr[num].ToString().Trim();
				}
			}
			return str;
		}

		public static string GetWhereByFlowId(CurrentUserEntity user, string flowid)
		{
			string tbid = flowid.Replace("bi_", "tb_").Replace("fw_", "tb_");
			string where = "where state=1 ";
			ComService comService = new ComService();

			string userid = user == null ? ";!NullEx;" : user.Account;
			string deptno = user == null ? ";!NullEx;" : user.DeptNo;

			/// 查看编辑模式 可在录入表管理-权限管理中设置
			///11查看编辑用户本人新建的数据
			///22查看用户所属单位(部门)新建的数据 编辑本人新建的数据
			///21查看编辑用户所属单位(部门)新建的数据
			///32查看用户所属下级部门新建的数据  编辑本人新建的数据
			///31查看编辑用户所属下级部门新建的数据
			///42查看所有数据  编辑本人新建的数据
			///41查看编辑所有数据
			string searchmode = "";
			DataTable dt = comService.GetDataTable("select SearchMode from flow where flowid='" + flowid + "'");
			if (dt.Rows.Count > 0)
			{
				searchmode = dt.Rows[0]["SearchMode"].ToString();
			}
			searchmode = string.IsNullOrEmpty(searchmode) ? "11" : searchmode;

			//基础信息表
			if (flowid.StartsWith("bi_"))
			{
				where = "where state=1";
				switch (searchmode.Substring(0, 1))
				{
					case "1"://查看编辑用户本人新建的数据
						where = "where (createUser = '" + userid + "') and (state=1) ";
						break;

					case "2"://查看用户所属单位(部门)新建的数据
						where = "where (createDept = '" + deptno + "') and (state=1) ";
						break;

					case "3"://查看用户所属下级部门新建的数据
						string str = ";" + deptno + ";";
						where = "where (createDeptStr  like'%" + str + "%') and (state=1) ";
						break;

					case "4"://查看所有数据
						where = "where (state=1) ";
						break;

					default:
						where = "where (state=1) ";
						break;
				}
			}
			else
			{
				where = "where (" + tbid + ".state=1)";
				switch (searchmode.Substring(0, 1))
				{
					case "1"://查看编辑用户本人新建的数据
						where = "where (FlowRunList.createUser = '" + userid + "') and (" + tbid + ".state=1) ";
						break;

					case "2"://查看用户所属单位(部门)新建的数据
						where = "where (FlowRunList.createDept = '" + deptno + "') and (" + tbid + ".state=1) ";
						break;

					case "3"://查看用户所属下级部门新建的数据
						string str = ";" + deptno + ";";
						where = "where (FlowRunList.createDeptStr  like'%" + str + "%') and (" + tbid + ".state=1) ";
						break;

					case "4"://查看所有数据
						where = "where (" + tbid + ".state=1) ";
						break;

					default:
						where = "where (" + tbid + ".state=1) ";
						break;
				}
			}

			return where;
		}

		public static string GetSearchMode(string flowid)
		{
			string searchmode = "";
			ComService comService = new ComService();

			DataTable dt = comService.GetDataTable("select SearchMode from flow where flowid='" + flowid + "'");
			if (dt.Rows.Count > 0)
			{
				searchmode = dt.Rows[0]["SearchMode"].ToString();
			}
			searchmode = string.IsNullOrEmpty(searchmode) ? "11" : searchmode;

			string some = "";
			switch (searchmode)
			{
				case "11":
					some = "【可查看本人新建的数据 || 可编辑删除本人新建的数据】";
					break;

				case "22":
					some = "【可查看所属单位(部门)用户新建的数据 || 可编辑删除本人新建的数据】";
					break;

				case "21":
					some = "【可查看所属单位(部门)用户新建的数据 || 可编辑删除所属单位(部门)用户新建的数据】";
					break;

				case "32":
					some = "【可查看下级单位(部门)用户新建的数据 || 可编辑删除本人新建的数据】";
					break;

				case "31":
					some = "【可查看下级单位(部门)用户新建的数据 || 可编辑删除下级单位(部门)用户新建的数据】";
					break;

				case "42":
					some = "【可查看所有数据 || 可编辑删除本人新建的数据】";
					break;

				case "41":
					some = "【可查看所有数据 || 可编辑删除所有数据】";
					break;

				default:
					some = "";
					break;
			}

			return some;
		}

		/// <summary>
		///
		/// </summary>
		public static object GetParamObject(CurrentUserEntity user)
		{
			if (user == null)
			{
				return new { sysusername = "!NullEx", sysuserid = "!NullEx", sysdeptno = "!NullEx", sysdeptname = "!NullEx", sysroleno = "!NullEx", sysrolename = "!NullEx", sysday = DateTime.Now.ToShortDateString(), systimenow = DateTime.Now };
			}
			else
			{
				return new { sysusername = user.RealName, sysuserid = user.Account, sysdeptno = user.DeptNo, sysdeptname = user.DeptName, sysroleno = user.RoleNo, sysrolename = user.RoleName, sysday = DateTime.Now.ToShortDateString(), systimenow = DateTime.Now };
			}
		}

		/// <summary>
		/// 取值
		/// </summary>
		/// <returns></returns>
		public static string GetTextFromList(IEnumerable<valueTextEntity> list, string classid, string value)
		{
			string str = "";
			foreach (valueTextEntity item in list)
			{
				if (item.ClassID == classid && item.Value == value)
				{
					str = item.Text == null ? "" : item.Text;
					break;
				}
			}

			return str;
		}

		/// <summary>
		/// 取值
		/// </summary>
		/// <returns></returns>
		public static string GetTextFromList(IEnumerable<ControlEntity> list, string controlid)
		{
			string str = "";
			foreach (ControlEntity item in list)
			{
				if (item.ControlID == controlid)
				{
					str = item.ControlValue == null ? "" : item.ControlValue;
					break;
				}
			}

			return str;
		}

		/// <summary>
		/// 取值
		/// </summary>
		/// <returns></returns>
		public static int? GetTextFromListToInt(IEnumerable<ControlEntity> list, string controlid)
		{
			string str = "";
			foreach (ControlEntity item in list)
			{
				if (item.ControlID == controlid)
				{
					str = item.ControlValue == null ? "" : item.ControlValue;
					break;
				}
			}

			return ExtConvert.ToIntOrNull(str);
		}

		/// <summary>
		/// 取值
		/// </summary>
		/// <returns></returns>
		public static decimal? GetTextFromListToDecimal(IEnumerable<ControlEntity> list, string controlid)
		{
			string str = "";
			foreach (ControlEntity item in list)
			{
				if (item.ControlID == controlid)
				{
					str = item.ControlValue == null ? "" : item.ControlValue;
					break;
				}
			}

			return ExtConvert.ToDecimalOrNull(str);
		}

		/// <summary>
		/// 取值
		/// </summary>
		/// <returns></returns>
		public static DateTime? GetTextFromListToDate(IEnumerable<ControlEntity> list, string controlid)
		{
			string str = "";
			foreach (ControlEntity item in list)
			{
				if (item.ControlID == controlid)
				{
					str = item.ControlValue == null ? "" : item.ControlValue;
					break;
				}
			}

			return ExtConvert.ToDateOrNull(str);
		}

		public static void GetRowAndIdByFlag(string flag, ref string id, ref string row)
		{
			string[] arr = BaseUtil.GetStrArray(flag, "#");

			id = arr[1].ToString();
			if (arr[2].ToString() == "0")
			{
				row = " 当前行";
			}
			else
			{
				row = " 第" + arr[2].ToString() + "行";
			}
		}

		public static int GetRowByFlag(string flag)
		{
			int row = 0;
			if (!string.IsNullOrEmpty(flag))
			{
				string[] arr = BaseUtil.GetStrArray(flag, "#");
				if (arr[2] != null)
				{
					row = int.Parse(arr[2].ToString());
				}
			}
			return row;
		}

		public static void GetTbAndIdByFlag(string flag, ref string tbid, ref string id)
		{
			string[] arr = BaseUtil.GetStrArray(flag, "#");

			id = arr[1].ToString();
			tbid = arr[0].ToString();
		}
	}
}